<link rel="stylesheet" type="text/css" href="/lib/zTree_v3/css/zTreeStyle/zTreeStyle.css">
<script type="text/javascript" src="/lib/zTree_v3/js/jquery.ztree.core.js"></script>
<script type="text/javascript" src="/lib/zTree_v3/js/jquery.ztree.excheck.js"></script>

<!-- 该页面只负责生成初始的数据结构,不包含任何勾选内容 -->
<div class="layui-row layui-col-space10" style="position: relative;overflow: hidden;">
    <div class="layui-inline"  style="height: 440px;width:96%;overflow-y: scroll;overflow-x: scroll;">
            <ul id="tree" class="ztree" style="padding: 16px;"></ul>
    </div>
    <button class="layui-btn"
    style="position:absolute;top:90%;right:6%;width:80px;height:30px;line-height:10px;border:none;outline:none;background-color:#1E9FFF;color:white;font-size: 17px;"
    onclick="Departmentconfirm()">确认</button>
</div>
<style>
    .layui-layer-content{
         overflow: hidden !important;
     }
  </style>
<script>

    var previewEmjio = " 👀"
    var downloadEmjio = " 🔽"


    /*1 发送请求,获取树结构数据*/
    shared.ajaxGet('/admin/tree/getPermissionTree', {}, function (res) {
        if (res) {
            generateTree(res)
        }
    })


    var treeObj
    /*2 通过树结构数据,生成树*/
    function generateTree(res) {

        var setting = {
            check: {
                enable: true,
                chkboxType: { "Y": "ps", "N": "ps" },
                // chkStyle: "checkbox",//显示 checkbox 选择框，默认checkbox可选择值radio

            },
            view: {
                showIcon: false
            },
            data: {
                simpleData: {
                    enable: true,
                    // idKey: "id",
                    // pIdKey: "pid"
                },
                key: {
                    title: "", //
                    name: "name"
                }
            },
            callback: {
                onCheck: onCheck
            }
        };

        var zTree = $.fn.zTree.init($("#tree"), setting, res);

        treeObj = $.fn.zTree.getZTreeObj("tree");
        /*以上,树结构初始化完毕*/


        /*以下,对该打钩数据权限的进行打钩操作*/
        for (let index = 0; index < dataPermissionList.length; index++) {
            const element = dataPermissionList[index];
            if (element.split("-").length == 1) {
                var dbcode = element.split("-")[0]

                var nodes = treeObj.getNodes();
                var ArrayNodes = treeObj.transformToArray(nodes);

                ArrayNodes.forEach(element_ => {
                    if (element_.id == dbcode) {
                        treeObj.checkNode(element_, true, true);
                        return false
                    }
                });
            }

            else if (element.split("-").length == 2) {
                var dbcode = element.split("-")[0]
                var field = element.split("-")[1]

                var nodes = treeObj.getNodes();
                var ArrayNodes = treeObj.transformToArray(nodes);
                ArrayNodes.forEach(element_ => {
                    if (element_.id == field && element_.pId == dbcode) {
                        treeObj.checkNode(element_, true, true);
                        return false
                    }
                });
            }

            else if (element.split("-").length == 3) {
                var dbcode = element.split("-")[0]
                var field = element.split("-")[1]
                var pd = element.split("-")[2]

                var nodes = treeObj.getNodes();
                var ArrayNodes = treeObj.transformToArray(nodes);
                ArrayNodes.forEach(element_ => {
                    if (element_.id == pd && element_.pId == field && treeObj.getNodeByTId(element_.parentTId).pId == dbcode) {
                        treeObj.checkNode(element_, true, true);
                        return false
                    }
                });
            }
        }

        generateNewTag()
    }


    /*第二集节点,显示第三级的结果:避免点开第三级就能进行查看*/
    function generateNewTag(){
        function filter(node) {
            return (node.level == 1);
        }
        var filednodes = treeObj.getNodesByFilter(filter); // 查找节点集合
        for (let index = 0; index < filednodes.length; index++) {
            const filednode = filednodes[index];

            var children = filednode.children
            for (let index_ = 0; index_ < children.length; index_++) {
                const child = children[index_];
                var state = child.checked
                if(state){
                    if(child.name=="预览"){
                        filednode.name+= previewEmjio+child.name
                    }else{
                        filednode.name+= downloadEmjio+child.name
                    }
                }
            }
            
        }
        
    }

    function onCheck(event, treeId, treeNode) {
        if(treeNode.level==0){
            var state = treeNode.checked
            var fields = treeNode.children
            if(state){
                for (let index = 0; index < fields.length; index++) {
                    const element = fields[index];
                    element.name += previewEmjio+"预览"
                    element.name += downloadEmjio+"下载"
                    treeObj.updateNode(element);
                }
            }else{
                for (let index = 0; index < fields.length; index++) {
                    const element = fields[index];
                    element.name = element.name.split(" ")[0]
                    treeObj.updateNode(element);
                }
            }
            
        }

        else if(treeNode.level==1){
            var state = treeNode.checked
            if(state){
                treeNode.name += previewEmjio+"预览"
                treeNode.name += downloadEmjio+"下载"
            }else{
                treeNode.name = treeNode.name.split(" ")[0]
            }
            
            treeObj.updateNode(treeNode);
        }

        else if(treeNode.level==2){
            console.log(treeNode)
            if(treeNode.name == "预览"){
                var state = treeNode.checked
                var parent = treeNode.getParentNode()
                if(state){
                    parent.name += previewEmjio+"预览"
                }else{
                    parent.name = parent.name.replace(previewEmjio+"预览", "")
                }
                treeObj.updateNode(parent);
            }else{
                var state = treeNode.checked
                var parent = treeNode.getParentNode()
                if(state){
                    parent.name += downloadEmjio+"下载"
                }else{
                    parent.name = parent.name.replace(downloadEmjio+"下载", "")
                }
                treeObj.updateNode(parent);
            }
            
        }
        // alert(treeNode.tId + ", " + treeNode.name + "," + treeNode.checked);
    };

    /*点击确认按钮*/
    function Departmentconfirm(){

        dataPermissionList = []
        var treeObj = $.fn.zTree.getZTreeObj("tree");
        var nodes = treeObj.getNodes();
        rec(nodes);
        layer.close(layer.index); 

    }

    function rec(nodes,preID) {
        for (let index = 0; index < nodes.length; index++) {
            const element = nodes[index];
            var state = element.check_Child_State
            var currID = element.id

            var idNew = ""
            if (!preID) {
                idNew = currID
            } else {
                idNew = preID + "-" + currID
            }

            var children = element.children
            if (state == 2) {
                //非最底层 且 子节点全选
                dataPermissionList.push(idNew)
            }
            else if (state == 1) {
                //非最底层 且 子节点部分选
                rec(children, idNew)
            }
            else if (state == 0) {
                //非最底层 且 子节点全未选
            } else {
                //最底层(无子节点)
                if (element.checked) {
                    dataPermissionList.push(idNew)
                }
            }
        }
    }

</script>